<template>
  <div class="login_container">
      <van-nav-bar class="page-navbar" title="登录/注册">
        <template #left>
          <van-icon
            name="cross"
            @click="$router.back()"
          ></van-icon>
        </template>
      </van-nav-bar>
      <van-form @submit="onLogin" :show-error-message="false"
      validate-first :show-error="false" @failed="onFailed" ref="loginForm">
       <van-field
          v-model="user.mobile"
          left-icon="shouji"
          icon-prefix="zhangyu"
          center
          name="mobile"
          placeholder="请输入手机号"
          :rules="formRules.mobile"
        ></van-field>
        <van-field
          v-model="user.code"
          center
          left-icon="yanzhengma"
          icon-prefix="zhangyu"
          name="code"
          placeholder="请输入验证码"
          :rules="formRules.code"
        >
        <template #button>
            <van-count-down :time="1000*60"  format="ss 秒" v-if="isCountDown" @finish="isCountDown=false"/>
            <van-button size="small" type="primary" round :loading="isLoading"  @click.prevent="onSendSms" v-else>发送验证码</van-button>
        </template>
        </van-field>
        <div class="login-btn-wrap">
          <van-button type="info" class="login-btn" block native-type="submit">登录</van-button>
        </div>
      </van-form>
  </div>
</template>
<script>
import { login, sendSms } from '@/apis/user'
export default {
  name: 'Login',
  data () {
    return {
      user: {
        mobile: '',
        code: ''
      },
      isLoading: false,
      isCountDown: false,
      formRules: {
        mobile: [
          { required: true, message: '请输入手机号' },
          { pattern: /^1[3|4|5|7|8][0-9]{9}$/, message: '请输入正确的手机号' }
        ],
        code: [
          { required: true, message: '请输入验证码' },
          { pattern: /^\d{6}$/, message: '验证码格式错误' }
        ]
      }
    }
  },
  methods: {
    async onLogin () {
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0
      })
      try {
        const { data } = await login(this.user)
        this.$toast.success('登录成功')
        // 保存用户登录状态
        this.$store.commit('SET_TOKEN', data.data)
        this.$store.commit('removeCachePage', 'LayoutIndex')
        const redirect = this.$route.query.redirect || '/'
        this.$router.replace(redirect)
      } catch (error) {
        this.$toast.fail('登录失败')
      }
    },
    onFailed (error) {
      if (error.errors[0]) {
        this.$toast({
          message: error.errors[0].message,
          position: top
        })
      }
    },
    async onSendSms () {
      try {
        await this.$refs.loginForm.validate('mobile')
        this.isLoading = true
        await sendSms(this.user.mobile)
        this.isCountDown = true
      } catch (error) {
        var message = ''
        if (error.name === 'mobile') {
          message = error.message
        } else if (error && error.response && error.response.status === 429) {
          message = '验证码发送太频繁了'
        } else {
          message = '未知错误'
        }
        this.$toast({
          message: message,
          position: top
        })
      }
      this.isLoading = false // 关闭倒计时loading
    }
  }
}
</script>
<style lang="less" scoped>
  .login-btn-wrap{
    padding: 26px 16px;
    .login-btn{
      background-color: #6db4fb;
      border: none;
      .van-button__text{
        font-size: 15px;
      }
    }
  }
</style>
